﻿using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text;
using System.Threading;
using OrderCms.DataAccess;
using OrderCms.DataOperation;
using OrderCms.Utilities;

namespace 订单分流.Api
{
	// Token: 0x02000009 RID: 9
	internal class PayClass
	{
		// Token: 0x06000027 RID: 39 RVA: 0x00004638 File Offset: 0x00002838
		public void StartPay()
		{
			while (Appconst.threadFlag)
			{
				try
				{
					this.orders = new DataTable();
					string SqlExt = string.Empty;
					this.threadStopwatch1.Restart();
					StringBuilder sql = new StringBuilder();
					sql.AppendLine("SELECT TOP " + Appconst.threadNum);
					sql.AppendLine("[ID],[User_GroupId],[OrderNumber],[RechargeNumber],[RechargeNum],[RechargeAmount],[AllAmount],[CorpId],[Order_TypeId],[ProvinceId],[CityId],[OrderStatus],[OrderTimeOutSec],[User_MemberId],[RechargeIp],[OrderGuid],[CreateTime]");
					sql.AppendLine("FROM [dbo].[Order_List] WITH(ROWLOCK, UPDLOCK, READPAST,INDEX(IX_Order_List_订单状态_分流使用))");
					sql.AppendLine(string.Concat(new object[]
					{
						"WHERE (OrderStatus=0 AND LastUpTime>='",
						DateTime.Now.AddHours(-24.0),
						"' or OrderStatus=3 AND LastUpTime<='",
						DateTime.Now.AddSeconds(-30.0),
						"')"
					}));
					this.orders = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
					this.获取订单耗时 = this.threadStopwatch1.ElapsedMilliseconds;
					Appconst.GetThreadPoolNum();
					bool flag = this.orders.Rows.Count == 0 && Appconst.threadFlag;
					if (flag)
					{
						Thread.Sleep(1000);
					}
					else
					{
						bool flag2 = this.orders.Rows.Count > 0;
						if (flag2)
						{
							this.threadStopwatch2.Restart();
							for (int i = 0; i < this.orders.Rows.Count; i++)
							{
								ThreadPool.QueueUserWorkItem(new WaitCallback(this.StartOrder), this.orders.Rows[i]);
							}
							while (Appconst.GetThreadPoolNum() != 0)
							{
								Thread.Sleep(10);
							}
							this.处理订单耗时 = this.threadStopwatch2.ElapsedMilliseconds;
							Appconst.ShowLog(string.Concat(new object[]
							{
								"获取耗时:",
								this.获取订单耗时,
								" 处理耗时:",
								this.处理订单耗时
							}));
						}
					}
				}
				catch (Exception ex)
				{
					PayClass.log.Error("获取分流订单失败", ex);
					Appconst.ShowLog("获取分流订单失败" + ex.Message);
					Thread.Sleep(5000);
				}
			}
			Appconst.ShowLog("该线程已停止");
		}

		// Token: 0x06000028 RID: 40 RVA: 0x000048B8 File Offset: 0x00002AB8
		public void StartOrder(object obj)
		{
			try
			{
				this.threadStopwatch1.Restart();
				DataRow orderListDr = (DataRow)obj;
				int Order_ListId = Convert.ToInt32(orderListDr["ID"]);
				int User_GroupId = Convert.ToInt32(orderListDr["User_GroupId"]);
				string OrderNumber = Convert.ToString(orderListDr["OrderNumber"]);
				string RechargeNumber = Convert.ToString(orderListDr["RechargeNumber"]);
				int RechargeNum = Convert.ToInt32(orderListDr["RechargeNum"]);
				decimal RechargeAmount = Convert.ToDecimal(orderListDr["RechargeAmount"]);
				decimal AllAmount = Convert.ToDecimal(orderListDr["AllAmount"]);
				int CorpId = Convert.ToInt32(orderListDr["CorpId"]);
				int Order_TypeId = Convert.ToInt32(orderListDr["Order_TypeId"]);
				int ProvinceId = Convert.ToInt32(orderListDr["ProvinceId"]);
				int CityId = Convert.ToInt32(orderListDr["CityId"]);
				int OrderStatus = Convert.ToInt32(orderListDr["OrderStatus"]);
				int User_MemberId = Convert.ToInt32(orderListDr["User_MemberId"]);
				int OrderTimeOutSec = Convert.ToInt32(orderListDr["OrderTimeOutSec"]);
				string OrderGuid = Convert.ToString(orderListDr["OrderGuid"]);
				DateTime CreateTime = Convert.ToDateTime(orderListDr["CreateTime"]);
				DateTime nowTime = DateTime.Now;
				Order_List_SetV2 order_List_Set = new Order_List_SetV2();
				Order_List_Shunt order_List_Shunt = new Order_List_Shunt();
				string RetMsg = string.Empty;
				StringBuilder sql = new StringBuilder();
				SqlParameter[] Parameters = new SqlParameter[]
				{
					new SqlParameter("@User_GroupId", SqlDbType.Int),
					new SqlParameter("@RechargeNumber", SqlDbType.VarChar)
				};
				Parameters[0].Value = User_GroupId;
				Parameters[1].Value = RechargeNumber;
				sql.AppendLine("SELECT COUNT(*) FROM [dbo].[BlackList] WHERE RechargeNumber=@RechargeNumber AND User_GroupId=@User_GroupId");
				int BlackListNum = DbHelper.GetSingle(CommandType.Text, sql.ToString(), Parameters);
				bool flag = BlackListNum > 0;
				if (flag)
				{
					this.threadStopwatch1.Restart();
					order_List_Set.OrderGuid = OrderGuid;
					order_List_Set.Order_ListId = Order_ListId;
					order_List_Set.LastOrderStatus = (EnumOrderStatus)OrderStatus;
					order_List_Set.UpdateOrderStatus = EnumOrderStatus.充值失败;
					order_List_Set.Order_SetStatus();
					RetMsg = "充值失败-" + order_List_Set.RetMsg;
				}
				else
				{
					int LimitNumberTime = 0;
					string LimitStartNumber = RechargeNumber;
					bool flag2 = RechargeNumber.Length >= 3;
					if (flag2)
					{
						LimitStartNumber = RechargeNumber.Substring(0, 3);
					}
					int Sup_ProductListId = this.GetSupProductId(Order_ListId, User_MemberId, User_GroupId, Order_TypeId, CorpId, ProvinceId, CityId, AllAmount, LimitStartNumber, out LimitNumberTime);
					int UserTimeOutSec = DateTimeHelper.DiffSec(CreateTime, nowTime);
					bool flag3 = UserTimeOutSec >= 172800;
					if (flag3)
					{
						Sup_ProductListId = 0;
					}
					int LimitNumberNum = 0;
					bool flag4 = LimitNumberTime != 0 && Sup_ProductListId > 0;
					if (flag4)
					{
						LimitNumberNum = this.GetRechargeNumberCount(orderListDr, RechargeNumber, User_GroupId, LimitNumberTime);
					}
					this.查询产品耗时 = this.threadStopwatch1.ElapsedMilliseconds;
					this.threadStopwatch1.Restart();
					bool flag5 = Sup_ProductListId <= 0 || (UserTimeOutSec >= OrderTimeOutSec && OrderTimeOutSec != 0);
					if (flag5)
					{
						this.threadStopwatch1.Restart();
						order_List_Set.OrderGuid = OrderGuid;
						order_List_Set.Order_ListId = Order_ListId;
						order_List_Set.LastOrderStatus = (EnumOrderStatus)OrderStatus;
						order_List_Set.UpdateOrderStatus = EnumOrderStatus.充值失败;
						order_List_Set.Order_SetStatus();
						RetMsg = "充值失败-" + order_List_Set.RetMsg;
					}
					else
					{
						bool flag6 = Sup_ProductListId <= 0 || LimitNumberNum != 0;
						if (flag6)
						{
							this.threadStopwatch1.Restart();
							order_List_Set.OrderGuid = OrderGuid;
							order_List_Set.Order_ListId = Order_ListId;
							order_List_Set.LastOrderStatus = (EnumOrderStatus)OrderStatus;
							order_List_Set.UpdateOrderStatus = EnumOrderStatus.延时分流;
							order_List_Set.Order_SetStatus();
							RetMsg = "延时分流-" + order_List_Set.RetMsg;
						}
						else
						{
							order_List_Shunt.User_MemberId = User_MemberId;
							order_List_Shunt.Order_ListId = Order_ListId;
							order_List_Shunt.Sup_ProductListId = Sup_ProductListId;
							order_List_Shunt.LastOrderStatus = (EnumOrderStatus)OrderStatus;
							order_List_Shunt.AllAmount = AllAmount;
							order_List_Shunt.Order_Shunt();
							RetMsg = "分流订单-" + order_List_Shunt.RetMsg;
							bool flag7 = order_List_Shunt.RetCode == 0 && order_List_Shunt.RetMsg == "找不到产品";
							if (flag7)
							{
								order_List_Set.OrderGuid = OrderGuid;
								order_List_Set.Order_ListId = Order_ListId;
								order_List_Set.Msg = order_List_Shunt.RetMsg;
								order_List_Set.LastOrderStatus = (EnumOrderStatus)OrderStatus;
								order_List_Set.UpdateOrderStatus = EnumOrderStatus.充值失败;
								order_List_Set.Order_SetStatus();
							}
						}
					}
				}
				this.修改订单耗时 = this.threadStopwatch1.ElapsedMilliseconds;
				Appconst.ShowLog(string.Concat(new object[]
				{
					"订单号：",
					OrderNumber,
					" 操作:",
					RetMsg,
					"\r\n查询耗时:",
					this.查询产品耗时,
					" 修改耗时:",
					this.修改订单耗时
				}));
			}
			catch (Exception ex)
			{
				PayClass.log.Error("修改分流订单失败", ex);
				Appconst.ShowLog("修改分流订单失败" + ex.Message);
				Thread.Sleep(5000);
			}
		}

		// Token: 0x06000029 RID: 41 RVA: 0x00004DB4 File Offset: 0x00002FB4
		private int GetShuntOrderTimeOut()
		{
			return 300;
		}

		// Token: 0x0600002A RID: 42 RVA: 0x00004DCC File Offset: 0x00002FCC
		private int GetSupProductId(int Order_ListId, int User_MemberId, int User_GroupId, int Order_TypeId, int CorpId, int ProvinceId, int CityId, decimal AllAmount, string LimitStartNumber, out int LimitNumberTime)
		{
			LimitNumberTime = 0;
			ArrayList arrayListNotIn = new ArrayList();
			ArrayList arrayListIn = new ArrayList();
			ArrayList arrayProductListNotIn = new ArrayList();
			StringBuilder sql = new StringBuilder();
			sql.AppendLine("SELECT User_SupId FROM [dbo].[Order_SupList] c");
			sql.AppendLine("WHERE c.Order_ListId=" + Order_ListId);
			DataTable Order_SupListDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
			for (int i = 0; i < Order_SupListDt.Rows.Count; i++)
			{
				int User_SupId = Convert.ToInt32(Order_SupListDt.Rows[i]["User_SupId"]);
				bool flag = !arrayListNotIn.Contains(User_SupId);
				if (flag)
				{
					arrayListNotIn.Add(User_SupId);
				}
				sql = new StringBuilder();
				sql.AppendLine("SELECT SupGroup FROM [dbo].[User_Sup] c WITH(NOLOCK)");
				sql.AppendLine("WHERE c.Id=" + User_SupId);
				DataTable User_SupDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
				bool flag2 = User_SupDt.Rows.Count > 0;
				if (flag2)
				{
					string SupGroup = User_SupDt.Rows[0]["SupGroup"].ToString();
					bool flag3 = !string.IsNullOrEmpty(SupGroup);
					if (flag3)
					{
						SqlParameter[] Parameters = new SqlParameter[]
						{
							new SqlParameter("@SupGroup", SqlDbType.VarChar),
							new SqlParameter("@User_GroupId", SqlDbType.Int)
						};
						Parameters[0].Value = SupGroup;
						Parameters[1].Value = User_GroupId;
						sql = new StringBuilder();
						sql.AppendLine("SELECT ID FROM [dbo].[User_Sup] c WITH(NOLOCK)");
						sql.AppendLine("WHERE c.SupGroup=@SupGroup AND c.User_GroupId=@User_GroupId");
						User_SupDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), Parameters);
						for (int j = 0; j < User_SupDt.Rows.Count; j++)
						{
							int SupGroupUser_SupId = Convert.ToInt32(User_SupDt.Rows[j]["ID"]);
							bool flag4 = !arrayListNotIn.Contains(SupGroupUser_SupId);
							if (flag4)
							{
								arrayListNotIn.Add(SupGroupUser_SupId);
							}
						}
					}
				}
			}
			sql = new StringBuilder();
			sql.AppendLine("SELECT User_SupId FROM [dbo].[User_Member_LimitUserSup] d");
			sql.AppendLine("WHERE d.User_MemberId=" + User_MemberId);
			sql.AppendLine("AND d.LimitType=0");
			DataTable User_Member_LimitUserSupDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
			for (int k = 0; k < User_Member_LimitUserSupDt.Rows.Count; k++)
			{
				int User_SupId2 = Convert.ToInt32(User_Member_LimitUserSupDt.Rows[k]["User_SupId"]);
				bool flag5 = !arrayListNotIn.Contains(User_SupId2);
				if (flag5)
				{
					arrayListNotIn.Add(User_SupId2);
				}
			}
			sql = new StringBuilder();
			sql.AppendLine("SELECT User_SupId FROM [dbo].[User_Member_LimitUserSup] d");
			sql.AppendLine("WHERE d.User_MemberId=" + User_MemberId);
			sql.AppendLine("AND d.LimitType=1");
			User_Member_LimitUserSupDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
			for (int l = 0; l < User_Member_LimitUserSupDt.Rows.Count; l++)
			{
				int User_SupId3 = Convert.ToInt32(User_Member_LimitUserSupDt.Rows[l]["User_SupId"]);
				bool flag6 = !arrayListIn.Contains(User_SupId3);
				if (flag6)
				{
					arrayListIn.Add(User_SupId3);
				}
			}
			sql = new StringBuilder();
			sql.AppendLine("SELECT Sup_ProductListId FROM [dbo].[User_Member_LimitSup_ProductList] d");
			sql.AppendLine("WHERE d.User_MemberId=" + User_MemberId);
			sql.AppendLine("AND d.LimitType=0");
			User_Member_LimitUserSupDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
			for (int m = 0; m < User_Member_LimitUserSupDt.Rows.Count; m++)
			{
				int Sup_ProductListId = Convert.ToInt32(User_Member_LimitUserSupDt.Rows[m]["Sup_ProductListId"]);
				bool flag7 = !arrayProductListNotIn.Contains(Sup_ProductListId);
				if (flag7)
				{
					arrayProductListNotIn.Add(Sup_ProductListId);
				}
			}
			string notInStr = string.Empty;
			for (int n = 0; n < arrayListNotIn.Count; n++)
			{
				notInStr += arrayListNotIn[n].ToString();
				bool flag8 = n != arrayListNotIn.Count - 1;
				if (flag8)
				{
					notInStr += ",";
				}
			}
			string inStr = string.Empty;
			for (int i2 = 0; i2 < arrayListIn.Count; i2++)
			{
				inStr += arrayListIn[i2].ToString();
				bool flag9 = i2 != arrayListIn.Count - 1;
				if (flag9)
				{
					inStr += ",";
				}
			}
			string notInProductStr = string.Empty;
			for (int i3 = 0; i3 < arrayProductListNotIn.Count; i3++)
			{
				notInProductStr += arrayProductListNotIn[i3].ToString();
				bool flag10 = i3 != arrayProductListNotIn.Count - 1;
				if (flag10)
				{
					notInProductStr += ",";
				}
			}
			SqlParameter[] Sup_ProductListParameters = new SqlParameter[]
			{
				new SqlParameter("@LimitStartNumber", SqlDbType.VarChar)
			};
			Sup_ProductListParameters[0].Value = LimitStartNumber;
			sql = new StringBuilder();
			sql.AppendLine("SELECT a.ID,a.User_SupId, b.MaxNum,b.SupAmount,b.LimitNumberTime");
			sql.AppendLine("FROM [dbo].[Sup_ProductList] a WITH(NOLOCK)");
			sql.AppendLine("LEFT JOIN [dbo].[User_Sup] b WITH(NOLOCK) ON a.User_SupId=b.ID");
			sql.AppendLine("WHERE  a.User_GroupId = " + User_GroupId);
			sql.AppendLine("AND  a.Order_TypeId = " + Order_TypeId);
			sql.AppendLine("AND  a.CorpId = " + CorpId);
			sql.AppendLine("AND  (a.ProvinceId = " + ProvinceId + " OR ProvinceId = 1000)");
			sql.AppendLine("AND  (a.CityId = " + CityId + " OR ISNULL(a.CityId, '') = '' OR CityId = 1000)");
			bool flag11 = !string.IsNullOrEmpty(inStr);
			if (flag11)
			{
				sql.AppendLine("AND a.User_SupId in");
				sql.AppendLine("(");
				sql.AppendLine(inStr);
				sql.AppendLine(")");
			}
			bool flag12 = !string.IsNullOrEmpty(notInStr);
			if (flag12)
			{
				sql.AppendLine("AND a.User_SupId not in");
				sql.AppendLine("(");
				sql.AppendLine(notInStr);
				sql.AppendLine(")");
			}
			bool flag13 = !string.IsNullOrEmpty(notInProductStr);
			if (flag13)
			{
				sql.AppendLine("AND a.ID not in");
				sql.AppendLine("(");
				sql.AppendLine(notInProductStr);
				sql.AppendLine(")");
			}
			sql.AppendLine("AND  a.MinAmount <= '" + AllAmount + "'");
			sql.AppendLine("AND  a.MaxAmount >= '" + AllAmount + "'");
			sql.AppendLine("AND  (a.LimitAmount LIKE '%" + AllAmount.ToString("0.00") + "%' OR ISNULL(a.LimitAmount, '') = '')");
			sql.AppendLine("AND  (a.LimitStartNumber NOT LIKE '%'+@LimitStartNumber+'%' OR ISNULL(a.LimitStartNumber, '') = '')");
			sql.AppendLine("AND  a.ProductListIsActive = 'TRUE'");
			sql.AppendLine("AND  b.SupIsActive = 'TRUE'");
			sql.AppendLine("ORDER BY a.SortNumber DESC,a.Discount, a.LastUpTime");
			DataTable supProductDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), Sup_ProductListParameters);
			bool flag14 = supProductDt.Rows.Count > 0;
			int result;
			if (flag14)
			{
				foreach (object obj in supProductDt.Rows)
				{
					DataRow dr = (DataRow)obj;
					int User_SupId4 = Convert.ToInt32(dr["User_SupId"]);
					int MaxNum = Convert.ToInt32(dr["MaxNum"]);
					sql = new StringBuilder();
					sql.AppendLine("SELECT COUNT(*)");
					sql.AppendLine("FROM [dbo].[Order_List] WITH(NOLOCK,INDEX(IX_Order_List_供货商_分流使用))");
					sql.AppendLine("WHERE (OrderStatus>=5 AND OrderStatus<=9)");
					sql.AppendLine("AND User_SupId=" + User_SupId4);
					sql.AppendLine("AND LastUpTime>='" + DateTime.Now.AddHours(-48.0) + "'");
					int User_Sup_Order_Num = DbHelper.GetSingle(CommandType.Text, sql.ToString(), new DbParameter[0]);
					decimal SupMoney = Convert.ToDecimal(dr["SupAmount"]);
					bool flag15 = User_Sup_Order_Num >= MaxNum && MaxNum != 0;
					if (!flag15)
					{
						LimitNumberTime = Convert.ToInt32(dr["LimitNumberTime"]);
						return Convert.ToInt32(dr["ID"]);
					}
				}
				result = 0;
			}
			else
			{
				result = 0;
			}
			return result;
		}

		// Token: 0x0600002B RID: 43 RVA: 0x000056D8 File Offset: 0x000038D8
		private int GetRechargeNumberCount(DataRow orderListDr, string RechargeNumber, int User_GroupId, int Time)
		{
			DataRow[] drs = this.orders.Select("RechargeNumber='" + RechargeNumber + "'");
			bool flag = drs.Length > 1;
			if (flag)
			{
				int drIndex = this.orders.Rows.IndexOf(orderListDr);
				int nextIndex = this.orders.Rows.IndexOf(drs[0]);
				bool flag2 = drIndex != nextIndex;
				if (flag2)
				{
					return 1;
				}
			}
			StringBuilder sql = new StringBuilder();
			sql.AppendLine("SELECT COUNT(*) FROM [dbo].[Order_List]");
			sql.AppendLine(string.Concat(new object[]
			{
				"WHERE RechargeNumber='",
				RechargeNumber,
				"' AND User_GroupId='",
				User_GroupId,
				"' AND OrderStatus IN(6,7,8,9) OR RechargeNumber='",
				RechargeNumber,
				"' AND User_GroupId='",
				User_GroupId,
				"' AND OrderStatus>=100 AND LastUpTime>='",
				DateTime.Now.AddSeconds((double)(Time * -1)),
				"'"
			}));
			return DbHelper.GetSingle(CommandType.Text, sql.ToString(), new DbParameter[0]);
		}

		// Token: 0x0400002E RID: 46
		public static LogHelper log = LogFactory.GetLogger(typeof(PayClass));

		// Token: 0x0400002F RID: 47
		public static object lockObj = new object();

		// Token: 0x04000030 RID: 48
		private Stopwatch threadStopwatch1 = new Stopwatch();

		// Token: 0x04000031 RID: 49
		private Stopwatch threadStopwatch2 = new Stopwatch();

		// Token: 0x04000032 RID: 50
		private DataTable orders = new DataTable();

		// Token: 0x04000033 RID: 51
		private long 获取订单耗时 = 0L;

		// Token: 0x04000034 RID: 52
		private long 处理订单耗时 = 0L;

		// Token: 0x04000035 RID: 53
		private long 查询产品耗时 = 0L;

		// Token: 0x04000036 RID: 54
		private long 修改订单耗时 = 0L;
	}
}
